//
//  CodeViewController.m
//  pocketmedicine
//
//  Created by student on 16/4/22.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "CodeViewController.h"
#import "MedicineModel.h"
#import "MedicineCell.h"
#import "AnalysisHelper.h"
#import "MHScanViewController.h"
#import "MHDefine.h"
#import <SVProgressHUD.h>
@interface CodeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *codeTableView;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;

@end

enum {
    SCANRESULTLBL = 1000,
    TIPSLBL,
    SCANBTN,
    
};

@implementation CodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.codeTableView.delegate = self;
    self.codeTableView.dataSource = self;
    self.codeTableView.hidden = YES;
    
   
}

//跳转
- (IBAction)cancel:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
    [SVProgressHUD dismiss];
}


- (IBAction)btnPressed:(id)sender {
    [self.codeTextField resignFirstResponder];
    NSString *httpUrl = @"http://www.tngou.net/api/drug/code";
    NSString *httpArg = [NSString stringWithFormat:@"code=%@",self.codeTextField.text];
    
    [self request:httpUrl withHttpArg:httpArg];

    
    
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.codeTextField resignFirstResponder];
}
- (void)request:(NSString *)httpUrl withHttpArg:(NSString *)httpArg
{
    [SVProgressHUD showWithStatus:@"正在搜索"];
    
    
    // 网址字符串
    NSString *urlStr = [[NSString alloc]initWithFormat: @"%@?%@", httpUrl, httpArg];
    // 网址
    NSURL *url = [NSURL URLWithString: urlStr];
    // 请求
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
    // GET请求
    [request setHTTPMethod: @"POST"];
    // 设置请求头
    [request addValue: @"639a5ad1fd83cbabd5b467f9201a5c8c" forHTTPHeaderField: @"apikey"];
    [NSURLConnection sendAsynchronousRequest: request
                                       queue: [NSOperationQueue mainQueue]
                           completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
                               if (error) {
                                   NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code);
                                   [SVProgressHUD showWithStatus:@"搜索错误"];
                                   self.codeTableView.hidden = YES;
                               } else {
                                   NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
                                   NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                   NSLog(@"HttpResponseCode:%ld", responseCode);
                                   NSLog(@"HttpResponseBody %@",responseString);
                                   
                                   NSDictionary *dict = [AnalysisHelper analysisInfo:responseString];

                                   self.medicine = [MedicineModel medicineWithDict:dict];
                                   NSLog(@"%@",self.medicine);
                                   if (self.medicine.name!=nil) {
                                      self.codeTableView.hidden = NO;
                                        [SVProgressHUD dismiss];
                                   }else{
                                       [SVProgressHUD showWithStatus:@"搜索错误"];
                                       self.codeTableView.hidden = YES;
                                   }
                                   [self.codeTableView reloadData];
                                   
                                   
                                   
                               }
                           }];
    

    
}





-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MedicineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.medicine = self.medicine;
    
    
    return cell;
    
}

- (IBAction)scanAction:(id)sender
{
    MHScanViewController *scanVC = [[MHScanViewController alloc] init];
    scanVC.rebackData = ^(NSString *retStr) {
        NSLog(@"回传过来的的扫面结果%@", retStr);

        NSString *httpUrl = @"http://www.tngou.net/api/drug/code";
        NSString *httpArg = [NSString stringWithFormat:@"code=%@",retStr];
        
        [self request:httpUrl withHttpArg:httpArg];
        self.codeTableView.hidden = NO;
        
    };
    [self presentViewController:scanVC animated:YES completion:nil];
    
    
}




@end
